﻿Public Class CalcConsumptionService

  Private Shared mInstance As CalcConsumptionService
  Public Shared Property Instance() As CalcConsumptionService
    Get
      If mInstance Is Nothing Then mInstance = New CalcConsumptionService
      Return mInstance
    End Get
    Set(ByVal value As CalcConsumptionService)
      mInstance = value
    End Set
  End Property

  Public Function CalcAverageConsumptionToLast(ByVal lValue As DailyConsumptionVM) As Double
    Dim lCurrentIndex As Integer = TotalConsumptionsVM.Instance.Consumptions.IndexOf(lValue)
    Dim lLast As DailyConsumptionVM

    If lCurrentIndex - 1 >= 0 Then
      lLast = TotalConsumptionsVM.Instance.Consumptions(lCurrentIndex - 1)
    Else
      lLast = TotalConsumptionsVM.Instance.Consumptions(lCurrentIndex)
    End If

    Dim lAverage As Double
    lAverage = (lValue.Consumption - lLast.Consumption) / (lValue.DateDC - lLast.DateDC).TotalHours
    Return lAverage
  End Function

  Public Function DurationToLast(ByVal lValue As DailyConsumptionVM) As TimeSpan
    Dim lCurrentIndex As Integer = TotalConsumptionsVM.Instance.Consumptions.IndexOf(lValue)
    Dim lLast As DailyConsumptionVM

    If lCurrentIndex - 1 >= 0 Then
      lLast = TotalConsumptionsVM.Instance.Consumptions(lCurrentIndex - 1)
    Else
      lLast = TotalConsumptionsVM.Instance.Consumptions(lCurrentIndex)
    End If

    Dim lAverage As TimeSpan
    lAverage = (lValue.DateDC - lLast.DateDC)
    Return lAverage
  End Function

End Class
